package cn.tedu.ems.service.impl;

import cn.tedu.ems.ex.ServiceException;
import cn.tedu.ems.mapper.CourseMapper;
import cn.tedu.ems.mapper.TeacherAndCourseMapper;
import cn.tedu.ems.mapper.TeacherMapper;
import cn.tedu.ems.pojo.dto.TeacherDTO;
import cn.tedu.ems.pojo.dto.TeacherUpdateDTO;
import cn.tedu.ems.pojo.entity.Teacher;
import cn.tedu.ems.pojo.entity.TeacherAndCourse;
import cn.tedu.ems.pojo.vo.CourseStandardVO;
import cn.tedu.ems.pojo.vo.TeacherAndCourseListVO;
import cn.tedu.ems.pojo.vo.TeacherDetailVO;
import cn.tedu.ems.pojo.vo.TeacherListVO;
import cn.tedu.ems.service.ITeacherService;
import cn.tedu.ems.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

/**
 * 这是教师的业务层接口实现类
 *
 * @Author java@Wqy
 * @Version 0.0.1
 */
@Slf4j
@Service
public class TeacherServiceImpl implements ITeacherService {
    // 引入配置文件中的变量
    @Value("${image.dirPath}")
    private String dirPath;

    // 注入教师持久层接口
    @Autowired
    private TeacherMapper teacherMapper;

    // 注入课程持久层接口
    @Autowired
    private CourseMapper courseMapper;

    // 注入教师课程关联持久层接口
    @Autowired
    private TeacherAndCourseMapper teacherAndCourseMapper;

    public TeacherServiceImpl() {
        log.debug("创建业务层接口实现类:TeacherServiceImpl");
    }

    /**
     * 处理添加教师的业务
     */
    @Override
    public void insert(TeacherDTO teacherDTO) {
        log.debug("开始处理添加教师的业务,参数:{}", teacherDTO);
        TeacherDetailVO teacherDetailVO = teacherMapper.selectByJobId(teacherDTO.getJobId());
        if (teacherDetailVO != null) {
            String message = "添加失败,该教师已存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }

        CourseStandardVO standardById = courseMapper.getStandardById(teacherDTO.getCourseId());
        log.debug("查询到的课程详情:{}",standardById);

        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(teacherDTO, teacher);
        teacher.setCourseName(standardById.getName());
        //给班级id赋值
        teacher.setGradeId(2);
        log.debug("即将向表中插入教师数据...参数:{}", teacher);
        int rows = teacherMapper.insert(teacher);
        if (rows > 1) {
            String message = "添加失败,服务器忙,请稍后再试...";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }

        // 向教师课程关联表插入信息(一位老师对应一个课程id)
        TeacherAndCourse teacherAndCourse = new TeacherAndCourse();
        teacherAndCourse.setTeacherId(teacher.getId());
        teacherAndCourse.setCourseId(teacherDTO.getCourseId());
        log.debug("即将向教师课程关联表中插入数据...");
        rows = teacherAndCourseMapper.insert(teacherAndCourse);
        if (rows > 1) {
            String message = "添加关联数据失败,服务器忙,请稍后再试...";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    /**
     * 开始处理删除教师的业务
     */
    @Override
    public void deleteById(Long id) {
        log.debug("开始处理根据id删除教师的业务,参数:{}", id);
        TeacherDetailVO teacherDetailVO = teacherMapper.selectById(id);
        if (teacherDetailVO == null) {
            String message = "删除失败,该数据不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
        log.debug("即将执行删除操作...");
        if (teacherDetailVO.getUrl().startsWith("http")){
            int rows = teacherMapper.deleteById(id);
            if (rows > 1) {
                String message = "删除失败,服务器忙,请稍后再试...";
                log.debug(message);
                throw new ServiceException(ServiceCode.ERR_DELETE, message);
            }
        }
        if (new File(dirPath + teacherDetailVO.getUrl()).delete()) {
            int rows = teacherMapper.deleteById(id);
            if (rows > 1) {
                String message = "删除失败,服务器忙,请稍后再试...";
                log.debug(message);
                throw new ServiceException(ServiceCode.ERR_DELETE, message);
            }
        }
    }

    /**
     * 根据id修改教师信息
     *
     * @param teacherUpdateDTO 教师信息
     */
    @Override
    public void updateById(Long id, TeacherUpdateDTO teacherUpdateDTO) {
        log.debug("开始处理根据id修改教师的业务,参数:{}", id);
        TeacherDetailVO teacherDetailVO = teacherMapper.selectById(id);
        if (teacherDetailVO == null) {
            String message = "删除失败,教师数据不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
        // 修改关联表中的数据
        TeacherAndCourse queryTeacherAndCourse = teacherAndCourseMapper.selectByTeacherIdAndCourseId(id);
        if (queryTeacherAndCourse == null){
            String message = "修改失败,关联数据不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        log.debug("开始修改关联表的信息...");
        TeacherAndCourse teacherAndCourse = new TeacherAndCourse();
        teacherAndCourse.setId(queryTeacherAndCourse.getId());
        teacherAndCourse.setTeacherId(id);
        teacherAndCourse.setCourseId(teacherUpdateDTO.getCourseId());
        int rows = teacherAndCourseMapper.update(teacherAndCourse);
        if (rows > 1) {
            String message = "修改关联信息失败,服务器忙,请稍后再试...";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }

        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(teacherUpdateDTO, teacher);
        if (teacherUpdateDTO.getCourseId() !=null){
            teacher.setCourseName(courseMapper.getStandardById(teacherUpdateDTO.getCourseId()).getName());
        }
        teacher.setId(id);
        log.debug("即将向数据库中修改教师信息,参数:{}", teacher);
        int rowsToTeacher = teacherMapper.updateById(teacher);
        if (rowsToTeacher > 1) {
            String message = "修改失败,服务器忙,请稍后再试...";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    /**
     * 开始处理根据id查询详情的业务
     *
     * @return
     */
    @Override
    public TeacherDetailVO selectDetail(String name, String jobId) {
        log.debug("开始处理根据姓名[{}]和职工Id[{}]查询教师详情的业务", name, jobId);
        int count = teacherMapper.countByName(name);
        if (count == 0) {
            String message = "查询失败,该教师姓名不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        TeacherDetailVO teacherDetailVO = teacherMapper.selectByJobId(jobId);
        if (teacherDetailVO == null) {
            String message = "查询失败,该教师职工id不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        log.debug("即将查询教师信息...");
        return teacherMapper.selectDetail(name, jobId);
    }

    /**
     * 根据id查询教师详情
     *
     * @param id 教师id
     * @return 返回详情VO类
     */
    @Override
    public TeacherDetailVO selectById(Long id) {
        log.debug("开始处理根据教师id查询教师详情的业务,参数:{}", id);
        TeacherDetailVO teacherDetailVO = teacherMapper.selectById(id);
        if (teacherDetailVO == null) {
            String message = "查询失败,该数据不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_SELECT, message);
        }
        return teacherDetailVO;
    }

    /**
     * 处理查询教师列表的业务
     *
     * @return 返回业务列表List集合
     */
    @Override
    public List<TeacherListVO> select() {
        log.debug("开始处理查询教师列表的业务...");
        return teacherMapper.select();
    }

    /**
     * 处理模糊查询教师列表的功能
     * @param wd 模糊字段
     * @return 返回列表
     */
    @Override
    public List<TeacherListVO> selectWd(String wd) {
        log.debug("开始处理模糊查询教师列表的功能,模糊字段:{}",wd);
        return teacherMapper.selectWd(wd);
    }

    /**
     * 处理查询教师与课程关联表的列表授课信息
     * @return 返回列表集合
     */
    @Override
    public List<TeacherAndCourseListVO> selectTCList() {
        log.debug("开始处理查询教师与课程关联表的列表授课信息,无参!");
        return teacherMapper.selectTCList();
    }

    /**
     * 处理模糊字段查询教师与课程关联表的列表授课信息
     * @param wd 模糊字段
     * @return 返回列表集合
     */
    @Override
    public List<TeacherAndCourseListVO> selectWdToTC(String wd) {
        log.debug("开始处理模糊字段查询教师与课程关联表的列表授课信息,模糊信息:{}",wd);
        return teacherMapper.selectWdToTC(wd);
    }
}
